package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.DeviceAccessCode;
import com.ruoyi.web.domain.dto.DeviceAccessCodeDto;
import com.ruoyi.web.domain.dto.GetAuthUrlDto;
import com.ruoyi.web.service.IDeviceAccessCodeService;
import com.ruoyi.web.service.IMonitorAreaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "访问码控制层")
@RequestMapping("/accessCode")
public class DeviceAccessCodeController extends BaseController {

    @Autowired
    private IDeviceAccessCodeService accessCodeService;


    @GetMapping("/page")
    public TableDataInfo list(DeviceAccessCodeDto condition)
    {
        startPage();
        List<DeviceAccessCode> deviceAccessCodes = accessCodeService.selectByCondition(condition);
        return getDataTable(deviceAccessCodes);
    }



    @Log(title = "访问码新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insert(@RequestBody DeviceAccessCode accessCode)
    {
        return accessCodeService.insertRecord(accessCode);

    }

    @Log(title = "访问码修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody DeviceAccessCode accessCode)
    {
        return accessCodeService.updateRecord(accessCode);

    }

    @Log(title = "访问码删除", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult delete(@RequestParam("baseId") Long baseId)
    {
        return accessCodeService.deleteRecord(baseId);

    }


    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam("baseId") Long baseId)
    {
        DeviceAccessCode detail = accessCodeService.detail(baseId);
        return AjaxResult.success(detail);
    }

    @GetMapping("/validate")
    public AjaxResult validateCode(@RequestParam("code") String code)
    {
       return accessCodeService.validateCode(code);

    }

    @GetMapping("/fillCodeFlag")
    public AjaxResult fillCodeFlag(HttpServletRequest request)
    {
        String ip = IpUtils.getIpAddr(request);
        return accessCodeService.fillCodeFlag(ip);
    }

    @GetMapping("/getAuthUrl")
    public AjaxResult getAuthUrl(GetAuthUrlDto dto)
    {
        return accessCodeService.getAuthUrl(dto);
    }

    /**
     * 客户拓展排行榜
     * @return
     */
    @GetMapping("/customerDevelopCount")
    public AjaxResult customerDevelopCount()
    {
        return accessCodeService.customerDevelopCount();
    }
}
